package org.liveboardgames.agricola.view.component
{
	import flash.events.MouseEvent;

	import mx.controls.Image;
	import mx.core.DragSource;
	import mx.managers.DragManager;

	import org.liveboardgames.agricola.control.delegates.helper.GameHelper;
	import org.liveboardgames.agricola.model.AgricolaModelLocator;
	import org.liveboardgames.agricola.model.enums.AbstractEnum;
	import org.liveboardgames.agricola.vo.Game;
	import org.liveboardgames.agricola.vo.common.Peep;
	import org.liveboardgames.agricola.vo.player.Player;

	public class PeepImage extends Image
	{
		private var _peep:Peep;

		private var _quantity:int;

		public function PeepImage()
		{
			super();
			this.setStyle("horizontalCenter", "0");
			this.setStyle("verticalCenter", "0");
			this.width = ImageHandler.PEEP_SIZE;
			this.addEventListener(MouseEvent.MOUSE_DOWN, dragIt);
		}

		override public function set width(value:Number):void
		{
			super.width = value;
			super.height = value;
		}

		override public function set height(value:Number):void
		{
			super.width = value;
			super.height = value;
		}

		public function set quantity(value:int):void
		{
			this._quantity = value;
			updateSource();
		}

		public function get quantity():int
		{
			return this._quantity;
		}

		public function set peep(value:Peep):void
		{
			this._peep = value;
			updateSource();
		}

		public function get peep():Peep
		{
			return this._peep;
		}

		private function updateSource():void
		{
			if (_peep != null && quantity > 0)
			{
				//var color:ColorEnum = _peep.owner.color;
				var color:String = GameHelper.getPeepOwner(AgricolaModelLocator.getInstance().game, _peep).color;
				if (_quantity > 1)
				{
					trace("The image for more than one peep is not here yet");
				}
				//this.source = ImageHandler.FARM_ELEMENTS_ROOT_PATH + color.name + "Peep_" + Math.min(_quantity, 3) + ".png";
				this.source = ImageHandler.FARM_ELEMENTS_ROOT_PATH + AbstractEnum.toTitleCase(color) + "Peep_" + Math.min(_quantity,
					3) + ".png";
				this.alpha = 0.8;
			}
		}

		private static function dragIt(event:MouseEvent):void
		{
			// Get the drag initiator component from the event object.
			var dragInitiator:PeepImage = event.currentTarget as PeepImage;

			var game:Game = AgricolaModelLocator.getInstance().game;
			var player:Player = GameHelper.getPeepOwner(game, dragInitiator.peep);

			// TODO: not really sure that this piece of logic should go there.
			// The view should delegate any piece of logic to the controller, but 
			// it seems there is no way to properly communicate via events
			if (GameHelper.isAllowedToPlayPeep(game, player))
			{
				// Create a DragSource object.
				var dragSource:DragSource = new DragSource();

				// Add the data to the object.
				dragSource.addData(dragInitiator.peep, 'peep');

				// Create a copy of the image to use as a drag proxy. Move only one peep at a time
				var dragProxy:PeepImage = new PeepImage();
				dragProxy.peep = dragInitiator.peep;
				dragProxy.quantity = 1;

				// Call the DragManager doDrag() method to start the drag.
				DragManager.doDrag(dragInitiator, dragSource, event, dragProxy);
			}
		}
	}
}